package internal

import (
	"server/GoUtil"
	"server/MergeConst"

	"server/pkg/github.com/name5566/leaf/gate"
	"server/pkg/github.com/name5566/leaf/log"
)

var Agents = make(map[gate.Agent]interface{})

func init() {
	skeleton.RegisterChanRPC("NewAgent", rpcNewAgent)
	skeleton.RegisterChanRPC("CloseAgent", rpcCloseAgent)
}

func rpcNewAgent(args []interface{}) {
	a := args[0].(gate.Agent)
	Agents[a] = struct{}{}
	log.Debug("rpcNewAgent")
	GoUtil.CallEvent(MergeConst.Notify_Socket_Connect, []interface{}{a})
}

func rpcCloseAgent(args []interface{}) {
	a := args[0].(gate.Agent)
	_, ok := Agents[a]
	if ok {
		GoUtil.CallEvent(MergeConst.Notify_Socket_Closed, []interface{}{Agents[a]})
		delete(Agents, a)
	}

	log.Debug("rpcCloseAgent")
}

func AsignPlayerToAgents(a gate.Agent, player interface{}) {
	Agents[a] = player
}
